package com.red.controller.user;


import com.red.dto.HomePageQueryDTO;
import com.red.dto.ProductsDTO;
import com.red.entity.Category;
import com.red.entity.Products;
import com.red.result.PageResult;
import com.red.result.Result;
import com.red.service.ProductsService;
import com.red.vo.ProductVO;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/product")
@ApiModel(value = "商品接口")
public class ProductsController {
    @Autowired
    private ProductsService productsService;

    @GetMapping("/productPort")
    @ApiModelProperty("商品分页查询")
    public Result<PageResult> productPage(HomePageQueryDTO dto){
        PageResult pageResult = productsService.pageQuery(dto);

        return Result.success(pageResult);
    }

    /**
     * 购物车购买商品
     * @param dto
     * @return
     */
    @PostMapping("/buy")
    @ApiModelProperty("购物车购买商品")
    public Result buyProduct(@RequestBody ProductsDTO dto){
        productsService.buyProduct(dto.getIds());
        return Result.success();
    }

    /**
     * 购买一件商品
     * @param id
     * @return
     */
    @PostMapping("/buyOne/{id}")
    @ApiModelProperty("购买一件商品")
    public Result buyOne(@PathVariable Long id){
        productsService.buyOne(id);
        return Result.success();
    }

    /**
     * 根据商品查id
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiModelProperty("根据商品查id")
    public Result<Products> selectById(@PathVariable Long id){
        Products products = productsService.selectById(id);
        return Result.success(products);
    }

    /**
     * 查看所有分类
     * @return
     */
    @GetMapping("/getCategoryAll")
    public Result<List<Category>> selectGetCategoryAll(){
        List<Category> list = productsService.selectList();
        return Result.success(list);
    }

    /**
     * 根据分类Id查商品
     * @param id
     * @return
     */
    @GetMapping("/selectByCategoryId/{id}")
    public Result<List<Products>> selectByCategoryId(@PathVariable Long id){
        List<Products> list = productsService.selectByCategoryId(id);
        return Result.success(list);

    }
}
